package org.mspring.mlog.dao.impl;

import org.mspring.mlog.dao.ArticleEntityDao;
import org.mspring.mlog.entity.ArticleEntity;
import org.mspring.mlog.common.constants.ArticleStatus;
import org.mspring.nbee.common.PageResult;
import org.mspring.nbee.common.Pager;
import org.mspring.nbee.db.dao.callback.SqlQueryCallback;
import org.mspring.nbee.db.dao.impl.BaseDao;
import org.mspring.nbee.db.dao.impl.EntitySequenceHandler;
import org.mspring.nbee.db.dao.sql.Sql;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gao Youbo
 * @since 2014-12-04 17:58:26
 */
@Repository
public class ArticleEntityDaoImpl extends BaseDao<ArticleEntity> implements ArticleEntityDao {
    @Override
    public int getArticleViewCount(long articleId) {
        Sql sql = new Sql("select view_count from t_article where id = ?", articleId);
        return executeQuery(sql, new SqlQueryCallback<Integer>() {
            @Override
            public Integer execute(ResultSet rs) throws SQLException {
                if (rs.next()) {
                    return rs.getInt("view_count");
                }
                return -1;
            }
        });
    }

    @Override
    public boolean updateArticleViewCount(long articleId, int viewCount) {
        Sql sql = new Sql("update t_article set view_count = ? where id = ?", viewCount, articleId);
        return executeUpdate(sql) > 0;
    }

    @Override
    public PageResult<Long> findArticleByCategory(long categoryId, int page) {
        Pager pager = new Pager(page, 20);
        long count = super.countBySql(new Sql("select count(*) from t_article where category = ? and status = ?", categoryId, ArticleStatus.NORMAL));
        List<Long> itemList = super.executeQuery(
                new Sql("select id from t_article where category = ? and status = ? order by id desc limit ? , ?", categoryId, ArticleStatus.NORMAL, pager.getOffset(), pager.getLimit()),
                new SqlQueryCallback<List<Long>>() {
                    @Override
                    public List<Long> execute(ResultSet rs) throws SQLException {
                        List<Long> result = new ArrayList<>();
                        while (rs.next()) {
                            result.add(rs.getLong(1));
                        }
                        return result;
                    }
                }
        );
        PageResult<Long> result = new PageResult<>();
        result.setPager(pager);
        result.setTotal(count);
        result.setItemList(itemList);
        return result;
    }

    @Override
    public PageResult<Long> findArticleByTag(long tagId, int page) {
        Pager pager = new Pager(page, 20);
        long count = super.countBySql(Sql.from("select count(article.id) from t_article article left join t_article_tag article_tag on article_tag.article_id = article.id where article_tag.tag_id = ? and article.status = ?", tagId, ArticleStatus.NORMAL));
        List<Long> itemList = super.executeQuery(
                Sql.from("select article.id from t_article article left join t_article_tag article_tag on article_tag.article_id = article.id where article_tag.tag_id = ? and article.status = ? limit ?,?", tagId, ArticleStatus.NORMAL, pager.getOffset(), pager.getLimit()),
                new SqlQueryCallback<List<Long>>() {
                    @Override
                    public List<Long> execute(ResultSet rs) throws SQLException {
                        List<Long> result = new ArrayList<>();
                        while (rs.next()) {
                            result.add(rs.getLong(1));
                        }
                        return result;
                    }
                }
        );
        PageResult<Long> result = new PageResult<>();
        result.setPager(pager);
        result.setTotal(count);
        result.setItemList(itemList);
        return result;
    }

    @Override
    public List<ArticleEntity> listLatestArticle(int limit) {
        Sql sql = new Sql("select * from t_article where status = ? order by id desc limit ?", ArticleStatus.NORMAL, limit);
        return listBySql(sql);
    }

    @Override
    public List<ArticleEntity> listHotArticle(int limit) {
        Sql sql = new Sql("select * from t_article where status = ? order by view_count desc limit ?", ArticleStatus.NORMAL, limit);
        return listBySql(sql);
    }

    @Override
    public void listById(EntitySequenceHandler<ArticleEntity> handler) {
        super.listById(null, null, handler);
    }
}
